/*
 * Copyright 2009 Johnson Consulting Services
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.johnsoncs.gwt.widgets.server.export.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.johnsoncs.gwt.widgets.client.dynamictable.export.ExportException;
import com.johnsoncs.gwt.widgets.server.export.ExportStorage;

/**
 * ExportStorage implementation that writes exported files to a specified path within the webapp
 * Should only be used for initial testing purposes, as it will not work in clustered environments 
 * @author wilj
 *
 */
public class DiskStorage implements ExportStorage {
	private final String path;

	/**
	 * Creates a new DiskStorage
	 * @param path the directory path to which the exported files will be written
	 */
	public DiskStorage(final String path) {
		String s = path.trim();
		if (!s.endsWith("/")) {
			s += "/";
		}
		this.path = s;
	}

	/* (non-Javadoc)
	 * @see com.johnsoncs.gwt.widgets.server.export.ExportStorage#deleteExportResult(java.lang.String)
	 */
	@Override
	public void deleteExportResult(final String fileName) throws ExportException {
		try {
			final File file = new File(path + fileName);
			if (file.exists()) {
				file.delete();
			}
		} catch (final Exception e) {
			throw new ExportException("Unable to delete export result file: " + fileName, e);
		}
	}

	/* (non-Javadoc)
	 * @see com.johnsoncs.gwt.widgets.server.export.ExportStorage#finalizeExportResult(java.lang.String)
	 */
	@Override
	public void finalizeExportResult(final String fileName) throws ExportException {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.johnsoncs.gwt.widgets.server.export.ExportStorage#getExportResult(java.lang.String)
	 */
	@Override
	public InputStream getExportResult(final String fileName) throws ExportException {
		try {
			final File file = new File(path + fileName);
			InputStream result = null;
			if (file.exists()) {
				result = new FileInputStream(file);
			}
			return result;
		} catch (final Exception e) {
			throw new ExportException("Unable to open export result file: " + fileName, e);
		}
	}

	private void initExportDir() throws ExportException {
		final File f = new File(path);
		if (!f.exists() && !f.mkdirs()) {
			throw new ExportException("Unable to initialize target directory for path: " + path);
		}
	}

	/* (non-Javadoc)
	 * @see com.johnsoncs.gwt.widgets.server.export.ExportStorage#initExportResult(java.lang.String)
	 */
	@Override
	public OutputStream initExportResult(final String fileName) throws ExportException {
		try {

			initExportDir();

			final File file = new File(path + fileName);
			file.deleteOnExit();

			return new FileOutputStream(file);
		} catch (final IOException e) {
			throw new ExportException("Error creating storage handle", e);
		}
	}

}
